package com.shanyue.redwine.activity;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.KeyEvent;
import android.view.View;
import com.shanyue.redwine.BuildConfig;
import com.shanyue.redwine.R;
import com.shanyue.redwine.adapter.AdapterGuide;
import com.shanyue.redwine.adapter.DotAdapter;
import com.shanyue.redwine.asvp.AutoScrollViewPager;
import com.shanyue.redwine.databinding.ActivityGuideBinding;
import com.shanyue.redwine.utils.AppTaskManager;
import com.shanyue.redwine.utils.Constants;
import com.shanyue.redwine.utils.SPUtils;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by Huang on 2016/6/8.
 */
public class ActivityGuide extends ActivityBase<ActivityGuideBinding> implements View
        .OnClickListener {
    private static final int[] imgs = new int[]{R.mipmap.wel1, R.mipmap.wel2, R.mipmap.wel3};
    //    AutoScrollViewPager vp_pager;
    DotAdapter mDotAdapter;
    //    RecyclerView rv_dot;
    boolean mCanFinish = false;
    Timer mTimer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(mActivity, R.layout.activity_guide);
        mTimer = new Timer();
        mBinding.tvOver.setOnClickListener(this);
        mBinding.tvGetIn.setOnClickListener(this);
        mBinding.vpWel.setInterval(2000);
        mBinding.vpWel.startAutoScroll();
        mBinding.vpWel.setSlideBorderMode(AutoScrollViewPager.SLIDE_BORDER_MODE_TO_PARENT);
        mBinding.vpWel.setAdapter(new AdapterGuide(imgs, getContext()));
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mBinding.rvDot.setLayoutManager(manager);
        mDotAdapter = new DotAdapter(getContext(), imgs.length);
        mBinding.rvDot.setAdapter(mDotAdapter);
        mBinding.vpWel.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int
                    positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 2) {
                    mBinding.tvGetIn.setVisibility(View.VISIBLE);
                } else {
                    mBinding.tvGetIn.setVisibility(View.GONE);
                }
                mDotAdapter.setSelectPosition(position);
                if (position == imgs.length - 1) {
                    mCanFinish = true;
                    mTimer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            if (mCanFinish) {//再次验证当前页面是否为最后一个页面.如果不是 则不执行跳转
                                start();
                            }
                        }
                    }, 1800);
                } else {
                    mCanFinish = false;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private Context getContext() {
        return this;
    }


    private void start() {
        mBinding.vpWel.stopAutoScroll();
        mTimer.cancel();
//        Intent intent = new Intent(getContext(), ActivityMain.class);
//        if (getIntent().getExtras() != null) {
//            intent.putExtras(getIntent().getExtras());
//        }
//        startActivity(intent);
        AppTaskManager.getAppManager().finishActivity(mActivity);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_over) {
            start();
        } else if (v.getId() == R.id.tvGetIn) {
            start();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SPUtils.put(mActivity, Constants.SP_KEY_OLD_VERSION, BuildConfig.VERSION_CODE);
        AppTaskManager.getAppManager().finishActivity(mActivity);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            start();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
